/*
 * Created on Jan 16, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.uploadapi.zooomr.ui;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.MenuItem;
import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.ImageAttributesUI;
import org.scohen.juploadr.app.JUploadr;
import org.scohen.juploadr.app.MultiImageAttributesUI;
import org.scohen.juploadr.app.PhotoSet;
import org.scohen.juploadr.app.UploadImage;
import org.scohen.juploadr.ui.PopupData;
import org.scohen.juploadr.ui.PopupHandler;
import org.scohen.juploadr.uploadapi.zooomr.Messages;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ZooomrPopupHandler extends PopupHandler {
    // private static final String SEPARATOR = "---";
    // private static final String DISABLED = "___";
    private String REMOVE_UPLOADED_IMAGES = Messages
            .getString("juploadr.ui.popup.remove.uploaded"); //$NON-NLS-1$
    private String ENABLE_UPLOADED_IMAGES = Messages
            .getString("juploadr.ui.popup.reupload.uploaded"); //$NON-NLS-1$
    private String SELECT_ALL_PHOTOS = Messages
            .getString("juploadr.ui.popup.select.all"); //$NON-NLS-1$
    private String SELECT_NONE = Messages
            .getString("juploadr.ui.popup.deselect.all"); //$NON-NLS-1$
    private String INVERT_SELECTION = Messages
            .getString("juploadr.ui.popup.invert.selection"); //$NON-NLS-1$
    private String EDIT_SELECTED_PHOTOS = Messages
            .getString("juploadr.ui.popup.edit.selected"); //$NON-NLS-1$
    private String REMOVE_SELECTED_PHOTOS = Messages
            .getString("juploadr.ui.popup.remove.selected"); //$NON-NLS-1$

    private JUploadr uploader;

    public ZooomrPopupHandler(Decorations parent, JUploadr uploadr) {
        super(parent);
        this.uploader = uploadr;
        show();
    }

    protected void widgetSelected(SelectionEvent event) {
        MenuItem src = (MenuItem) event.widget;
        String action = src.getText();
        if (action.equals(REMOVE_UPLOADED_IMAGES)) {
            // remove all image meta data
            uploader.removeUploadedPhotos();
        } else if (SELECT_ALL_PHOTOS.equals(action)
                || SELECT_NONE.equals(action)) {
            boolean selected = SELECT_ALL_PHOTOS.equals(action);
            Iterator values = uploader.getImageAttributes().values().iterator();
            while (values.hasNext()) {
                ImageAttributes atts = (ImageAttributes) values.next();
                atts.setSelected(selected);
            }
            uploader.refresh();
        } else if (INVERT_SELECTION.equals(action)) {
            Iterator values = uploader.getImageAttributes().values().iterator();
            while (values.hasNext()) {
                ImageAttributes atts = (ImageAttributes) values.next();
                atts.setSelected(!atts.isSelected());
            }
            uploader.refresh();
        } else if (EDIT_SELECTED_PHOTOS.equals(action)) {
            List<ImageAttributes> atts = getSelectedPhotos();
            ImageAttributesUI ui;
            if (atts.size() == 1) {
                ui = new ImageAttributesUI((ImageAttributes) atts.get(0));
            } else {
                ui = new MultiImageAttributesUI(atts);
            }
            ui.show(parent.getDisplay());

        } else if (REMOVE_SELECTED_PHOTOS.equals(action)) {
            List atts = getSelectedPhotos();
            for (int i = 0; i < atts.size(); i++) {
                ImageAttributes att = (ImageAttributes) atts.get(i);
                att.removeFromWindow();
            }
            uploader.refresh();
        } else if (ENABLE_UPLOADED_IMAGES.equals(action)) {
            Iterator values = uploader.getImageAttributes().values().iterator();
            while (values.hasNext()) {
                ImageAttributes atts = (ImageAttributes) values.next();
                atts.setState(UploadImage.STATE_DATA_ENTERED);
                atts.getButton().setEnabled(true);
            }
            uploader.refresh();
        } else {
            // adding them to a set
            MenuItem item = (MenuItem) event.widget;
            PhotoSet set = (PhotoSet) item.getData();
            Iterator atts = uploader.getImageAttributes().values().iterator();
            while (atts.hasNext()) {
                ImageAttributes att = (ImageAttributes) atts.next();
                if (att.isSelected()) {
                    List alreadyAdded = att.getPhotoSets();
                    if (alreadyAdded.contains(set)) {
                        alreadyAdded.remove(set);
                    } else {
                        set.addPhoto(att);
                    }
                }
            }
            uploader.refresh();
        }
    }

    /**
     * @return
     */
    private List<ImageAttributes> getSelectedPhotos() {
        Iterator<ImageAttributes> values = uploader.getImageAttributes().values().iterator();
        List<ImageAttributes> atts = new LinkedList<ImageAttributes>();

        while (values.hasNext()) {
            ImageAttributes element = (ImageAttributes) values.next();
            if (element.isSelected()) {
                atts.add(element);
            }
        }
        return atts;
    }

    /**
     * 
     */
    protected void regenerateOptions() {
        options = new LinkedList<PopupData>();

        if (arePhotosSelected()) {
            options.add(new PopupData(EDIT_SELECTED_PHOTOS, true));
        } else {
            options.add(new PopupData(EDIT_SELECTED_PHOTOS, false));
        }
        options.add(new PopupData(SELECT_ALL_PHOTOS, true));
        options.add(new PopupData(SELECT_NONE, true));
        options.add(new PopupData(INVERT_SELECTION, true));
        options.add(PopupData.SEPARATOR);
        if (areAnyPhotosUploaded()) {
            options.add(new PopupData(REMOVE_UPLOADED_IMAGES, true));
            options.add(new PopupData(ENABLE_UPLOADED_IMAGES, true));
        } else {
            options.add(new PopupData(REMOVE_UPLOADED_IMAGES, false));
            options.add(new PopupData(ENABLE_UPLOADED_IMAGES, false));
        }

        if (arePhotosSelected()) {
            options.add(new PopupData(REMOVE_SELECTED_PHOTOS, true));
        } else {
            options.add(new PopupData(REMOVE_SELECTED_PHOTOS, false));
        }

    }

    private boolean arePhotosSelected() {
        Iterator iter = uploader.getImageAttributes().values().iterator();
        while (iter.hasNext()) {
            ImageAttributes next = (ImageAttributes) iter.next();
            if (next.isSelected()
                    && next.getState() != UploadImage.STATE_REMOVED) {
                return true;
            }
        }
        return false;
    }

    private boolean areAnyPhotosUploaded() {
        Iterator iter = uploader.getImageAttributes().values().iterator();
        while (iter.hasNext()) {
            UploadImage atts = (UploadImage) iter.next();
            if (atts.getState() == UploadImage.STATE_UPLOADED) {
                return true;
            }
        }
        return false;
    }

}